import pygame
import sys

# 初始化 Pygame
pygame.init()

# 定义常量
WIDTH, HEIGHT = 800, 600
FPS = 60
PLAYER_SPEED = 5
JUMP_POWER = 15
GRAVITY = 0.5

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("横板闯关游戏")
clock = pygame.time.Clock()

# 玩家类
class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface((30, 50))
        self.image.fill((0, 255, 0))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.vx = 0
        self.vy = 0
        self.on_ground = False

    def update(self):
        self.vx = 0
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.vx = -PLAYER_SPEED
        if keys[pygame.K_RIGHT]:
            self.vx = PLAYER_SPEED
        if keys[pygame.K_UP] and self.on_ground:
            self.vy = -JUMP_POWER
            self.on_ground = False

        self.vy += GRAVITY
        self.rect.x += self.vx
        self.rect.y += self.vy

        # 边界检测
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.bottom > HEIGHT:
            self.rect.bottom = HEIGHT
            self.vy = 0
            self.on_ground = True

# 障碍物类
class Obstacle(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        super().__init__()
        self.image = pygame.Surface((width, height))
        self.image.fill((255, 0, 0))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

# 终点类
class Finish(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        super().__init__()
        self.image = pygame.Surface((width, height))
        self.image.fill((0, 0, 255))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

# 创建精灵组
all_sprites = pygame.sprite.Group()
obstacles = pygame.sprite.Group()
finish = pygame.sprite.GroupSingle()

# 创建玩家
player = Player(50, HEIGHT - 100)
all_sprites.add(player)

# 创建障碍物
obstacle1 = Obstacle(200, HEIGHT - 150, 100, 20)
obstacle2 = Obstacle(400, HEIGHT - 200, 100, 20)
obstacles.add(obstacle1, obstacle2)
all_sprites.add(obstacle1, obstacle2)

# 创建终点
finish_point = Finish(WIDTH - 100, HEIGHT - 100, 50, 50)
finish.add(finish_point)
all_sprites.add(finish_point)

# 游戏主循环
running = True
while running:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 更新玩家状态
    all_sprites.update()

    # 碰撞检测：玩家与障碍物
    hits = pygame.sprite.spritecollide(player, obstacles, False)
    if hits:
        if player.vy > 0:
            player.rect.bottom = hits[0].rect.top
            player.vy = 0
            player.on_ground = True

    # 碰撞检测：玩家与终点
    if pygame.sprite.spritecollide(player, finish, False):
        print("恭喜你，闯关成功！")
        running = False

    # 绘制屏幕
    screen.fill((255, 255, 255))
    all_sprites.draw(screen)

    pygame.display.flip()

# 退出游戏
pygame.quit()
sys.exit()